package com.liang.study.design.service.logic;

import com.liang.study.design.model.vo.TreeNodeLink;

import java.util.List;
import java.util.Map;

/**
 * @author lsm
 * @ClassName BaseLogic.java
 * @Description TODO
 * @createTime 2023年12月21日 10:34:00
 */
public abstract class BaseLogic implements LogicFilter{

    @Override
    public Long logicFilter(String matterValue, List<TreeNodeLink> treeNodeLinkList) {
        for (TreeNodeLink treeNodeLink : treeNodeLinkList) {
            if (decisionLogic(matterValue, treeNodeLink)){
                return treeNodeLink.getNodeIdTo();
            }
        }
        return null;
    }

    @Override
    public abstract String getValueFromMap(Map<String, String> map) ;

    private boolean decisionLogic(String matterValue, TreeNodeLink nodeLink) {
        switch (nodeLink.getRuleLimitEnum()){
            case EQUAL:
                return matterValue.equals(nodeLink.getRuleLimitValue());
            case GREATER_THAN:
                return Double.parseDouble(matterValue)>Double.parseDouble(nodeLink.getRuleLimitValue());
            case LESS_THAN:
                return Double.parseDouble(matterValue)<Double.parseDouble(nodeLink.getRuleLimitValue());
            case BE_EQUAL_OR_GREATER_THAN:
                return Double.parseDouble(matterValue)>=Double.parseDouble(nodeLink.getRuleLimitValue());
            case BE_EQUAL_OR_LESS_THAN:
                return Double.parseDouble(matterValue)<=Double.parseDouble(nodeLink.getRuleLimitValue());
        }
        return false;
    }
}
